<script setup lang="ts">
import {markRaw, onMounted, ref} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import * as echarts from "echarts";
import {useGetPieDataApi} from "@/api"
import type {Pie} from "@/interface"

const pie = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const pieData = ref<Pie>({
  data:[]
})
const init = async () => {
  const {data:res} = await useGetPieDataApi()
  pieData.value = res
  myChart.value = markRaw(echarts.init(pie.value));
  draw();
}
const draw = () => {
  option.value = {
    title:{
      text:'2023年11月电影票房占比',
      left:'center',
      textStyle:{
        color:'#000000'
      }
    },
    tooltip:{
      formatter:'{a}<br/>{b}: {d}%'
    },
    legend: {
      top: 'bottom'
    },
    series: [
      {
        name: '票房占比',
        type: 'pie',
        radius: [50, 100],
        center: ['50%', '50%'],
        itemStyle: {
          borderRadius: 8
        },
        data: pieData.value.data
      }
    ]
  }
  myChart.value.setOption(option.value, {notMerge: true});
}
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="pie" class="pie">
  </div>
</template>

<style scoped>
.pie{
  width: 100%;
  height: 100%;
}
</style>